 package com.tnl.hunter.webchat.utils;
 import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.biz.system.config.WebchatServiceConstant;

 @Service
 public class SMSService
 {
   private static final Logger logger = LoggerFactory.getLogger(SMSService.class);
   
   public static String sendSMS(String phoneNum,String code) throws Exception
   {
     HttpClient client = new HttpClient();
     PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
     post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");
     
     NameValuePair[] data = { new NameValuePair("Uid", WebchatServiceConstant.smsYRCUid), 
    		 new NameValuePair("Key", WebchatServiceConstant.smsYRCKey), 
    		 new NameValuePair("smsMob", phoneNum), 
    		 new NameValuePair("smsText", WebchatServiceConstant.SMSCONTENT+code) 
     //new NameValuePair("smsText", companyName+"Hr刚刚在跳哪里平台上给你发起招聘/求职留言，请尽快查看.") 
     };
 
     post.setRequestBody(data);
     
     client.executeMethod(post);
     Header[] headers = post.getResponseHeaders();
     int statusCode = post.getStatusCode();
     logger.debug("statusCode:" + statusCode);
     for (Header h : headers) {
       logger.debug(h.toString());
     }
     String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
     
     logger.debug("result" + result);
     post.releaseConnection();
     return result;
   }
   
	public static String sendSMSTakerOrder(String phoneNum, String url, String content) throws Exception {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
		post.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=gbk");

		NameValuePair[] data = {
				new NameValuePair("Uid", WebchatServiceConstant.smsZDUid),
				new NameValuePair("Key", WebchatServiceConstant.smsZDKey),
				new NameValuePair("smsMob", phoneNum),
				new NameValuePair("smsText", content)
		};

		post.setRequestBody(data);

		client.executeMethod(post);
		Header[] headers = post.getResponseHeaders();
		int statusCode = post.getStatusCode();
		logger.debug("statusCode:" + statusCode);
		for (Header h : headers) {
			logger.debug(h.toString());
		}
		String result = new String(post.getResponseBodyAsString().getBytes(
				"gbk"));

		logger.debug("result" + result);
		post.releaseConnection();
		return result;
	}
	
	public static String sendSMSrefuseOrder(String phoneNum, String hunterName,String orderDesc) throws Exception {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
		post.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=gbk");

		NameValuePair[] data = {
				new NameValuePair("Uid", WebchatServiceConstant.smsZDUid),
				new NameValuePair("Key", WebchatServiceConstant.smsZDKey),
				new NameValuePair("smsMob", phoneNum),
				new NameValuePair("smsText", "您好，您预约的咨询项目，因顾问时间临时调整或其他原因，已被取消订单，您可以重新预约其他顾问。如有问题，可咨询18721386781")
		};

		post.setRequestBody(data);

		client.executeMethod(post);
		Header[] headers = post.getResponseHeaders();
		int statusCode = post.getStatusCode();
		logger.debug("statusCode:" + statusCode);
		for (Header h : headers) {
			logger.debug(h.toString());
		}
		String result = new String(post.getResponseBodyAsString().getBytes(
				"gbk"));

		logger.debug("result" + result);
		post.releaseConnection();
		return result;
	}
   
	public static String sendSMSconfirmOrder(String phoneNum, String hunterName) throws Exception {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
		post.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=gbk");
		hunterName=hunterName+"顾问";
		NameValuePair[] data = {
				new NameValuePair("Uid", WebchatServiceConstant.smsZDUid),
				new NameValuePair("Key", WebchatServiceConstant.smsZDKey),
				new NameValuePair("smsMob", phoneNum),
				new NameValuePair("smsText", "您预约的["+hunterName+"]咨询项目已完成，请尽快前往小程序-个人中心-我的咨询-进行中确认并反馈，感谢您的支持。")
	
		};

		post.setRequestBody(data);

		client.executeMethod(post);
		Header[] headers = post.getResponseHeaders();
		int statusCode = post.getStatusCode();
		logger.debug("statusCode:" + statusCode);
		for (Header h : headers) {
			logger.debug(h.toString());
		}
		String result = new String(post.getResponseBodyAsString().getBytes(
				"gbk"));

		logger.debug("result" + result);
		post.releaseConnection();
		return result;
	}
   public static String sendSMSInterview(String companyname,String tel, String time,String address,String contact) throws Exception
	   {
	     HttpClient client = new HttpClient();
	     PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
	     post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");
	     
	     NameValuePair[] data = { new NameValuePair("Uid", WebchatServiceConstant.smsYRCUid), 
	    		 new NameValuePair("Key", WebchatServiceConstant.smsYRCKey), 
	    		 new NameValuePair("smsMob", tel), 
	    		 new NameValuePair("smsText", "您好！"+companyname+"公司HR，我司通过[医人才]看到您投递职位，正式通知您来我司参加面試。1.面試时间："+time+"，2.面試地点："+address+"，3.面試官及联系方式："+contact+"；感谢您的信任和选择。") };// 设置短信内容
	     //new NameValuePair("smsText", companyName+"Hr刚刚在跳哪里平台上给你发起招聘/求职留言，请尽快查看.") 
	 
	     post.setRequestBody(data);
	     
	     client.executeMethod(post);
	     Header[] headers = post.getResponseHeaders();
	     int statusCode = post.getStatusCode();
	     logger.debug("statusCode:" + statusCode);
	     for (Header h : headers) {
	       logger.debug(h.toString());
	     }
	     String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
	     
	     logger.debug("result" + result);
	     post.releaseConnection();
	     return result;
	 }
   
   public static String sendCheckSMS(String username,String content) throws Exception
   {
     HttpClient client = new HttpClient();
     PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
     post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");
     
     NameValuePair[] data = { new NameValuePair("Uid", WebchatServiceConstant.smsYRCUid), 
    		 new NameValuePair("Key", WebchatServiceConstant.smsYRCKey), 
    		 new NameValuePair("smsMob", "18721386781"),
    		 new NameValuePair("smsText",username+"用户申请"+content+"账号，请审核") 
     };
 
     post.setRequestBody(data);
     
     client.executeMethod(post);
     Header[] headers = post.getResponseHeaders();
     int statusCode = post.getStatusCode();
     logger.debug("statusCode:" + statusCode);
     for (Header h : headers) {
       logger.debug(h.toString());
     }
     String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
     
     logger.debug("result" + result);
     post.releaseConnection();
     return result;
   }

	 public static String sendAskMsg(String tel,String content) throws Exception
	 {
		 HttpClient client = new HttpClient();
		 PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
		 post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");

		 NameValuePair[] data = { new NameValuePair("Uid", WebchatServiceConstant.smsYRCUid),
				 new NameValuePair("Key", WebchatServiceConstant.smsYRCKey),
				 new NameValuePair("smsMob", tel),
				 new NameValuePair("smsText",content)
		 };

		 post.setRequestBody(data);

		 client.executeMethod(post);
		 Header[] headers = post.getResponseHeaders();
		 int statusCode = post.getStatusCode();
		 logger.debug("statusCode:" + statusCode);
		 for (Header h : headers) {
			 logger.debug(h.toString());
		 }
		 String result = new String(post.getResponseBodyAsString().getBytes("gbk"));

		 logger.debug("result" + result);
		 post.releaseConnection();
		 return result;
	 }


   public static void main(String[] args) {
	   try {
		   sendAskMsg("13818401178", "测一下,您好! 感谢您應聘上海传瑞企业管理咨询有限公司,很遗憾的通知您未通过[公关经理]职位的评估,将您的简历储备到我司人才库中,后续如有合适职位,我们将会优先与您联系。再次感谢您对我们的关注!");
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   
   }
	
 }


